package com.sky.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.sky.dto.*;
import com.sky.entity.Orders;
import com.sky.entity.SalesTopList;
import com.sky.entity.User;
import com.sky.mapper.PeportMapper;
import com.sky.service.PeportService;
import com.sky.vo.OrderReportVO;
import com.sky.vo.SalesTop10ReportVO;
import com.sky.vo.TurnoverReportVO;
import com.sky.vo.UserReportVO;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class PeportServiceImpl implements PeportService {
    @Autowired
    private PeportMapper peportMapper;

    /*
     *
     *营业额
     * */
    @Override
    public TurnoverReportVO turnoverStatistics(LocalDate begin, LocalDate end) {
//        List<LocalDate> localDateList = localDateList(begin, end);
//        List<Double> amount = new ArrayList<>();
//        for (LocalDate localDate : localDateList) {
//            LocalDateTime localDateTimeDegin = LocalDateTime.of(localDate, LocalTime.MIN);
//            LocalDateTime localDateTimeEnd = LocalDateTime.of(localDate, LocalTime.MAX);
//            Map map = new HashMap();
//            map.put("begin", localDateTimeDegin);
//            map.put("end", localDateTimeEnd);
//            map.put("status", Orders.COMPLETED);
//            Double d = peportMapper.turnoverStatistics(map);
//            if (d == null) {
//                d = (double) 0;
//            }
//            amount.add(d);
//        }
//
//        String join = StringUtils.join(localDateList, ",");
//        String string = JSONObject.toJSONString(join);
//        String join1 = StringUtils.join(amount, ",");
//        String string1 = JSONObject.toJSONString(join1);
//
//        TurnoverReportVO turnoverReportVO = new TurnoverReportVO();
//        turnoverReportVO.setDateList(join);
//        turnoverReportVO.setTurnoverList(join1);
        List<TurnoverReportDTO> turnoverReportDTOS = peportMapper.turnoverStatistics(begin, end);
        List<LocalDate> LocalDates = turnoverReportDTOS.stream().map(TurnoverReportDTO::getLocalDate).collect(Collectors.toList());
        List<Double> collect = turnoverReportDTOS.stream().map(TurnoverReportDTO::getAmount).collect(Collectors.toList());
        TurnoverReportVO turnoverReportVO = new TurnoverReportVO();
        turnoverReportVO.setDateList(StringUtils.join(LocalDates, ","));
        turnoverReportVO.setTurnoverList(StringUtils.join(collect, ","));
        return turnoverReportVO;
    }

    /*
     * 用户统计
     * */
    @Override
    public UserReportVO userStatistics(LocalDate begin, LocalDate end) {
//        List<LocalDate> localDateList = localDateList(begin, end);
//        List<Integer> list = new ArrayList<>();
//        for (LocalDate localDate : localDateList) {
//            LocalDateTime localDateTimeDegin = LocalDateTime.of(localDate, LocalTime.MIN);
//            LocalDateTime localDateTimeEnd = LocalDateTime.of(localDate, LocalTime.MAX);
//            Map map = new HashMap();
//            map.put("begin", localDateTimeDegin);
//            map.put("end", localDateTimeEnd);
//            Integer integer = peportMapper.userStatistics(map);
//            if (integer == null) {
//                integer = 0;
//            }
//            list.add(integer);
//
//        }
//        List<Integer> usertIntList = new ArrayList<>();
//        Integer userint = peportMapper.userListStatistics();
//        for (LocalDate localDate : localDateList) {
//            usertIntList.add(userint);
//        }
//
//        String join = StringUtils.join(localDateList, ",");
//        String userjoin = StringUtils.join(list, ",");
//        String joinusertIntList = StringUtils.join(usertIntList, ",");
//        UserReportVO userReportVO = new UserReportVO();
//        userReportVO.setNewUserList(userjoin);
//        userReportVO.setDateList(join);
//
//        userReportVO.setTotalUserList(joinusertIntList);
        List<UserReportDTO> userReportDTOList = peportMapper.userStatistics(begin, end);
        List<LocalDate> dateList = userReportDTOList.stream().map(UserReportDTO::getLocalDate).collect(Collectors.toList());
        List<Integer> totalUserList = userReportDTOList.stream().map(UserReportDTO::getTotalUserList).collect(Collectors.toList());
        List<Integer> newUserList = userReportDTOList.stream().map(UserReportDTO::getNewUserList).collect(Collectors.toList());

        return UserReportVO.builder()
                .dateList(StringUtils.join(dateList,","))
                .totalUserList(StringUtils.join(totalUserList,","))
                .newUserList(StringUtils.join(newUserList,","))
                .build();
    }
    /*
     *订单统计
     * */

    @Override
    public OrderReportVO ordersStatistics(LocalDate begin, LocalDate end) {
//        List<LocalDate> localDateList = localDateList(begin, end);
//        Integer totalOrderCount = peportMapper.totalOrderCount();
//        Integer validOrderCount = peportMapper.validOrderCount();
//        Double orderCompletionRate = peportMapper.orderCompletionRate();
//        List<Integer> orderCountList = new ArrayList<>();
//
//        List<Integer> validOrderCountList = new ArrayList<>();
//
//        for (LocalDate localDate : localDateList) {
//            LocalDateTime localDateTimeDegin = LocalDateTime.of(localDate, LocalTime.MIN);
//            LocalDateTime localDateTimeEnd = LocalDateTime.of(localDate, LocalTime.MAX);
//            Map map = new HashMap();
//            map.put("begin", localDateTimeDegin);
//            map.put("end", localDateTimeEnd);
//            Integer integer1 = peportMapper.orderCountListlist(map);
//            map.put("status", Orders.COMPLETED);
//            Integer integer = peportMapper.validOrderCountList(map);
//            orderCountList.add(integer1);
//            validOrderCountList.add(integer);
//        }
//
//
//        String join = StringUtils.join(localDateList, ",");
//        String join1 = StringUtils.join(orderCountList, ",");
//        String join2 = StringUtils.join(validOrderCountList, ",");
//
//        OrderReportVO orderReportVO = new OrderReportVO();
//        orderReportVO.setDateList(join);
//        orderReportVO.setTotalOrderCount(totalOrderCount);
//        orderReportVO.setValidOrderCount(validOrderCount);
//        orderReportVO.setOrderCompletionRate(orderCompletionRate);
//        orderReportVO.setOrderCountList(join1);
//        orderReportVO.setValidOrderCountList(join2);
        Map map =new HashMap<>();
        map.put("begin",begin);
        map.put("end",end);
      List<OrderReportDTO> count =peportMapper.orderCount(map);
        List<Integer> orderCountList = count.stream().map(OrderReportDTO::getOrderCountList).collect(Collectors.toList());
        List<Integer> orderCountLists = count.stream().map(OrderReportDTO::getOrderCountList).collect(Collectors.toList());
        Integer totalOrderCount = count.get(0).getTotalOrderCount();
        map.put("status",Orders.COMPLETED);
        List<OrderReportDTO>  validOrderCount= peportMapper.validOrderCount(map);
        List<LocalDate> dateList = validOrderCount.stream().map(OrderReportDTO::getDateList).collect(Collectors.toList());
        List<Integer> validOrderCountLists = validOrderCount.stream().map(OrderReportDTO::getValidOrderCountList).collect(Collectors.toList());
        Integer validOrderIntage = validOrderCount.get(0).getValidOrderCount();
        BigDecimal bigDecimal1 = new BigDecimal( validOrderIntage.doubleValue() / totalOrderCount.doubleValue());
        double orderCompletionRate = bigDecimal1.setScale(3, BigDecimal.ROUND_HALF_UP).doubleValue();
        /*
    //日期，以逗号分隔，例如：2022-10-01,2022-10-02,2022-10-03
    private String dateList;

    //每日订单数，以逗号分隔，例如：260,210,215
    private String orderCountList;

    //每日有效订单数，以逗号分隔，例如：20,21,10
    private String validOrderCountList;

    //订单总数
    private Integer totalOrderCount;

    //有效订单数
    private Integer validOrderCount;

    //订单完成率
    private Double orderCompletionRate;
*/
        return OrderReportVO.builder()
                .dateList(StringUtils.join(dateList,","))
                .orderCountList(StringUtils.join(orderCountLists,","))
                .validOrderCountList(StringUtils.join(validOrderCountLists,","))
                .totalOrderCount(totalOrderCount)
                .validOrderCount(validOrderIntage)
                .orderCompletionRate(orderCompletionRate)
                .build();
    }

    /*
     * 菜品统计
     * */
    @Override
    public SalesTop10ReportVO SalesTop(LocalDate begin, LocalDate end) {
//        LocalDateTime localDateTimeDegin = LocalDateTime.of(begin, LocalTime.MIN);
//        LocalDateTime localDateTimeEnd = LocalDateTime.of(end, LocalTime.MAX);
//        List<GoodsSalesDTO> salesTopList = peportMapper.Salestop(localDateTimeDegin, localDateTimeEnd);
//
//        List<String> collect = null;
//        List<Integer> collect1 = null;
//        if (salesTopList != null) {
//            collect = salesTopList.stream().map(GoodsSalesDTO::getName).collect(Collectors.toList());
//            collect1 = salesTopList.stream().map(GoodsSalesDTO::getNumber).collect(Collectors.toList());
//        }
//        String join = StringUtils.join(collect, ',');
//
//        String join1 = StringUtils.join(collect1, ',');
//
//
//      SalesTop10ReportVO salesTop10ReportVO = new SalesTop10ReportVO(join, join1);
      List<SalesTop10ReportDTO> salesTop10ReportVO=  peportMapper.salesTop10ReportVO(begin,end);
        List<String> nameList = salesTop10ReportVO.stream().map(SalesTop10ReportDTO::getDishName).collect(Collectors.toList());
        List<Integer> numberList = salesTop10ReportVO.stream().map(SalesTop10ReportDTO::getDishNumber).collect(Collectors.toList());

        return SalesTop10ReportVO.builder()
                .nameList(StringUtils.join(nameList,","))
                .numberList(StringUtils.join(numberList,","))
                .build();
    }
//    private List<LocalDate> localDateList(LocalDate begin, LocalDate end) {
//        List<LocalDate> localDateList = new ArrayList<>();
//
//        localDateList.add(begin);
//        while (!begin.equals(end)) {
//            begin = begin.plusDays(1);
//            localDateList.add(begin);
//        }
//        return localDateList;
//    }
}
